package com.niit.xlztest.repository;

import com.niit.xlztest.bean.Book;
import com.niit.xlztest.bean.BorrowRecord;
import com.niit.xlztest.bean.Shopping_cart;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface BuyRepository extends JpaRepository<Shopping_cart,Integer> {

//    @Query("from Book  b where b.user_id = :user_id")
//    List<BorrowRecord> findUserId(@Param("user_id") Integer user_id);
//    @Modifying
//    @Transactional
//    @Query("delete from BorrowRecord b where b.borrow_id = :borrow_id")
//    void deleteBorrowRecord(Integer borrow_id);
//
//    @Modifying
//    @Transactional
//    @Query("update BorrowRecord b set b.return_date= current_timestamp where b.borrow_id = :borrow_id")
//    void updateBorrowDateByBookId(@Param("borrow_id") Integer borrow_id);

    @Modifying
    @Query("delete from Shopping_cart s where s.user_id = :userId and s.book_id = :bookId")
    void deleteByUserIdAndBookId(@Param("userId") Integer userId, @Param("bookId") Integer bookId);
    @Query("from Shopping_cart  S where S.user_id = :user_id")
    List<Shopping_cart> findAllBookInCart(Integer user_id);

    @Modifying
    @Query("UPDATE Book b SET b.book_sales = b.book_sales + 1 WHERE b.book_id = :book_id")
    void buyBook(@Param("book_id") Integer book_id);


}
